<?php

require_once("includes/pagetop.php");

if(!(isset($_SESSION['Allyfolio_User_ID']) && trim($_SESSION['Allyfolio_User_ID'])!=""))
{
    redirect("index.php");
    exit();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>AllyFolio | Search Friend </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />
<script src="js/jquery1.10.2.min.js"></script>
<script src="js/jquery.placeholder.js"></script>

<script src="js/general.js"></script>
<script src="js/functions.js"></script>

	

</script>

<script type="text/javascript">
    $(function() { $('input, textarea').placeholder();});
</script>
<style type="text/css">
    .placeholder {
      color: #cccccc;
      }
    .userpiclass
    {
        border:1px solid #999;
    }
    .friendrow
    {
        border-bottom: 1px solid #999;
        height:60px;
    }
</style>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>
    
</head>

<body>

	<div id="main_wrapper">
    <?php
    
    $pageheading="Friends";
    require_once("includes/header.php");
    
    
    ?>
    <?php
    $condition=array();
    $query="select af_users.*,Unique_User_Friend_ID from af_users,af_user_friends where af_users.User_ID=af_user_friends.User_ID and af_user_friends.status=3 and User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' ";
        
    $query.=" order by requestdate desc " ;
    
    $friendreqinfo=$pdodbobj->fetch_array($pdodbobj->query($query,array()));
    
    ?>
    <!--=================Body Contain Start=======================-->
    <form name="mainform" method="post">
    <div class="wrapper">
        <?php
        if(count($friendreqinfo)>0)
        {
            ?>
    	<div class="header">
            
        	<div class="contain-wrap">
            	<div style="width:63%">
                	<p class="dn_txt">Pending Friend Requests</p>
                </div>
                
                <div class="clearfix"></div>
            
            </div>
            
            <div class="clearfix"></div>
        </div>
        <table cellpadding="0" cellspacing="0" style="width: 980px;margin: 0 auto;">
            <tr>
                
                <td>
                    <table style="width: 100%;padding:2px;">
                    <?php
                    
                        for($i=0;$i<count($friendreqinfo);$i++)
                        {
                            $requestary="select Unique_User_Friend_ID,User_ID,User_Friend_ID,Status from af_user_friends where (User_ID=:fromuserid && User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' ) or (User_Friend_ID=:touserid && User_ID='".$_SESSION['Allyfolio_User_ID']."' )";
                            $checkstatus=$pdodbobj->fetch_array($pdodbobj->query($requestary,array("fromuserid"=>$friendreqinfo[$i]['User_ID'],"touserid"=>$friendreqinfo[$i]['User_ID'])));
                                                    
                        ?>
                            <tr>
                                <td class="friendrow" style="vertical-align: top;padding-bottom:5px;padding-top:5px;">
                                    <table style="width: 100%;">
                                        <tr>
                                            <td width="60" style="padding:0px;"><div class="noti_pic"><a href="dna.php?t=<?php echo $friendreqinfo[$i]['Allyfolio_User_ID']; ?>"> <img class="userpiclass" src="<?php if(trim($friendreqinfo[$i]['Profile_Pic_Path'])!=""){ echo $friendreqinfo[$i]['Profile_Pic_Path']; }else{ echo "images/default_user_pic.jpg"; } ?>" height="48px" width="45px" /></a></div></td>
                            				<td width="220" valign="top" style="text-align:left;">
                            				<div style="position:relative; top:-4px"><span class="noti_name" style="font-size:21px; color:#44c8f5 !important"><a href="dna.php?t=<?php echo $friendreqinfo[$i]['Allyfolio_User_ID']; ?>"><?php echo $friendreqinfo[$i]['Full_Name']; ?></p></a></span></div>
                            				<div class="separator"></div>
                            	               <span style="margin-top:5px; float:left; margin-right:5px; color:#44c8f5 !important"><?php echo getCommonFriends($pdodbobj,$friendreqinfo[$i]['User_ID'],$_SESSION['Allyfolio_User_ID'])." Common Friends"; ?></span>
                                            </td>
                            				<td width="138" align="left" id="requeststatus_<?php echo $i; ?>">
                            				
                                            	<table border="0" width="100%">
                                            	   <tr>
                                            			<td rowspan="2" valign="middle">
                                                            <input type="button" class="reject" value="Accept" onclick="return changeStatusFriendRequest(1,<?php echo $friendreqinfo[$i]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" />
                                                            <input type="button" class="reject" value="Reject"  onclick="return changeStatusFriendRequest(4,<?php echo $friendreqinfo[$i]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" />
                                                            <input type="button" class="reject" value="Block"  onclick="return changeStatusFriendRequest(0,<?php echo $friendreqinfo[$i]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" />
                                                        </td>
                                            		</tr>
                                            	</table>
                            			   </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php
                        }
                    
                    
                    ?>
                    </table>
                </td>
            </tr>
        </table>
        <?php
        }
        ?>
        
        
        
        <?php
        ################## Friends info ###########################
        
        $condition=array();
        $query="select af_users.*,af_user_friends.Status as request_status from af_users,af_user_friends where af_users.User_ID =af_user_friends.User_Friend_ID and (af_user_friends.Status=1 || af_user_friends.Status=0) and af_user_friends.User_ID ='".$_SESSION['Allyfolio_User_ID']."' and Profile_Status=0 ";
            
        $query.=" order by Full_Name desc " ;
        
        $friendsinfo=$pdodbobj->fetch_array($pdodbobj->query($query,array()));
        
        
        if(count($friendsinfo)>0)
        {
            ?>
    	<div class="header">
            
        	<div class="contain-wrap">
            	<div style="width:63%">
                	<p class="dn_txt">Friends List</p>
                </div>
                
                <div class="clearfix"></div>
            
            </div>
            
            <div class="clearfix"></div>
        </div>
        <table cellpadding="0" cellspacing="0" style="width: 980px;margin: 0 auto;">
            <tr>
                
                <td>
                    <table style="width: 100%;padding:2px;">
                    <?php
                    
                        for($i=0;$i<count($friendsinfo);$i++)
                        {
                                                   
                        ?>
                            <tr>
                                <td class="friendrow" style="vertical-align: top;padding-bottom:5px;padding-top:5px;">
                                    <table style="width: 100%;">
                                        <tr>
                                            <td width="60" style="padding:0px;"><div class="noti_pic"><a href="dna.php?t=<?php echo $friendsinfo[$i]['Allyfolio_User_ID']; ?>"> <img class="userpiclass" src="<?php if(trim($friendsinfo[$i]['Profile_Pic_Path'])!=""){ echo $friendsinfo[$i]['Profile_Pic_Path']; }else{ echo "images/default_user_pic.jpg"; } ?>" height="48px" width="45px" /></a></div></td>
                            				<td width="220" valign="top" style="text-align:left;">
                            				<div style="position:relative; top:-4px"><span class="noti_name" style="font-size:21px; color:#44c8f5 !important"><a href="dna.php?t=<?php echo $friendsinfo[$i]['Allyfolio_User_ID']; ?>"><?php echo $friendsinfo[$i]['Full_Name']; ?></p></a></span></div>
                            				<div class="separator"></div>
                            	               <span style="margin-top:5px; float:left; margin-right:5px; color:#44c8f5 !important"><?php echo getCommonFriends($pdodbobj,$friendsinfo[$i]['User_ID'],$_SESSION['Allyfolio_User_ID'])." Common Friends"; ?></span>
                                            </td>
                            				<td width="138" align="right" id="requesttd_<?php echo $i; ?>">
                            				
                                            	<table border="0" width="100%">
                                            	   <tr>
                                            			<td><input type="button" id="acceptbtn<?php echo $i; ?>" <?php if($friendsinfo[$i]['request_status']==1){ ?>style="display: none;"<?php } ?> class="reject" value="Unblock Friend"  onclick="return blockFriend('blockbtn','acceptbtn',1,<?php echo $_SESSION['Allyfolio_User_ID']; ?>,<?php echo $friendsinfo[$i]['User_ID'];  ?>,<?php echo $i; ?>);" />
                                                            <input type="button" id="blockbtn<?php echo $i; ?>" <?php if($friendsinfo[$i]['request_status']==0){ ?>style="display: none;"<?php } ?> class="reject" value="Block Friend"  onclick="return blockFriend('acceptbtn','blockbtn',0,<?php echo $_SESSION['Allyfolio_User_ID']; ?>,<?php echo $friendsinfo[$i]['User_ID'];  ?>,<?php echo $i; ?>);" />
                                                        </td>
                                                            
                                            		</tr>
                                            	</table>
                            			   </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php
                        }
                    
                    
                    ?>
                    </table>
                </td>
            </tr>
        </table>
        <?php
        }
        ?>
        
        
        
        <?php
        
        ################## Friends info ###########################
        
        $query="select distinct af_users.User_ID,Profile_Pic_Path,Full_Name,Allyfolio_User_ID from af_users,af_user_friends where Profile_Status=0 and (((af_users.User_ID=af_user_friends.User_ID) or (af_users.User_ID=af_user_friends.User_Friend_ID)) and af_user_friends.status=3) and af_users.User_ID not in ( select Active_Friends_IDs from af_users where User_ID='".$_SESSION['Allyfolio_User_ID']."') and af_users.User_ID!='".$_SESSION['Allyfolio_User_ID']."'
                            AND af_users.User_ID not in ( select User_ID from af_user_friends where User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' )
                             AND af_users.User_ID not in ( select User_Friend_ID from af_user_friends where User_ID='".$_SESSION['Allyfolio_User_ID']."' ) 
                            ";
        
        $query.=" order by Full_Name asc limit 20" ;
        
        $knownfriendsinfo=$pdodbobj->fetch_array($pdodbobj->query($query,array()));
        
        if(count($knownfriendsinfo)==0)
        {
            
            $query="select distinct af_users.User_ID,Profile_Pic_Path,Full_Name,Allyfolio_User_ID from af_users where Profile_Status=0 and af_users.User_ID!='".$_SESSION['Allyfolio_User_ID']."'
                    AND af_users.User_ID not in ( select User_ID from af_user_friends where User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' )
                     AND af_users.User_ID not in ( select User_Friend_ID from af_user_friends where User_ID='".$_SESSION['Allyfolio_User_ID']."' ) 
                    ";

            $query.=" order by rand(), Full_Name asc limit 20" ;
            $knownfriendsinfo=$pdodbobj->fetch_array($pdodbobj->query($query,array()));
            
        }
        if(count($knownfriendsinfo)>0)
        {
            ?>
    	<div class="header">
            
        	<div class="contain-wrap">
            	<div style="width:63%">
                	<p class="dn_txt">Peoples You may Know</p>
                </div>
                
                <div class="clearfix"></div>
            
            </div>
            
            <div class="clearfix"></div>
        </div>
        <table cellpadding="0" cellspacing="0" style="width: 980px;margin: 0 auto;">
            <tr>
                
                <td>
                    <table style="width: 100%;padding:2px;">
                    <?php
                    
                        for($i=0;$i<count($knownfriendsinfo);$i++)
                        {
                                                   
                        ?>
                            <tr>
                                <td class="friendrow" style="vertical-align: top;padding-bottom:5px;padding-top:5px;">
                                    <table style="width: 100%;">
                                        <tr>
                                            <td width="60" style="padding:0px;"><div class="noti_pic"><a href="dna.php?t=<?php echo $knownfriendsinfo[$i]['Allyfolio_User_ID']; ?>"> <img class="userpiclass" src="<?php if(trim($knownfriendsinfo[$i]['Profile_Pic_Path'])!=""){ echo $knownfriendsinfo[$i]['Profile_Pic_Path']; }else{ echo "images/default_user_pic.jpg"; } ?>" height="48px" width="45px" /></a></div></td>
                            				<td width="220" valign="top" style="text-align:left;">
                            				<div style="position:relative; top:-4px"><span class="noti_name" style="font-size:21px; color:#44c8f5 !important"><a href="dna.php?t=<?php echo $knownfriendsinfo[$i]['Allyfolio_User_ID']; ?>"><?php echo $knownfriendsinfo[$i]['Full_Name']; ?></p></a></span></div>
                            				<div class="separator"></div>
                            	               <span style="margin-top:5px; float:left; margin-right:5px; color:#44c8f5 !important"><?php echo getCommonFriends($pdodbobj,$knownfriendsinfo[$i]['User_ID'],$_SESSION['Allyfolio_User_ID'])." Common Friends"; ?></span>
                                            </td>
                            				<td width="138" align="left" id="requeststatus_<?php echo $i; ?>">
                            				
                                            	<table border="0" width="100%">
                                            	   <tr>
                                            			<td>
                                                            <input type="button" value="Send Request" onclick="return sendFriendRequest(<?php echo $knownfriendsinfo[$i]['User_ID']; ?>,<?php echo $i; ?>);" />
                                                        </td>
                                                            
                                            		</tr>
                                            	</table>
                            			   </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php
                        }
                    
                    
                    ?>
                    </table>
                </td>
            </tr>
        </table>
        <?php
        }
        ?>
        
    </div>
    <div class="clearfix"></div>
    </form>        
            
	</div>
    <div class="clearfix"></div>
    <?php
    require_once("includes/footer.php");
?>
<script lang="javascript">
//<![CDATA[
function sendFriendRequest(touserid,rowid)
{
    alertify.confirm("Are you sure, you want to send friend request?", function (e) {
		if (e) {
			alertify.success("You've clicked OK");
			$.ajax({
              cache:false,  
              async:false,
              url: "ajax_search/ajax_manageRequests.php?action=friendrequest&touserid="+touserid,
        	  success: function( data ) {
        	    if(trim(data)=="success")
                {
                    alertify.alert("You have successfully sent friend request.");
                    document.getElementById('requeststatus_'+rowid).innerHTML="<b>Friends Request Sent.</b>";
                    if(document.getElementById('alertify-cover'))
                    {
                        document.getElementById('alertify-cover').style.display='none';
                    }
                    return false;
                }
                
        	  }
        	});
		} else {
			alertify.error("You've clicked Cancel");
		}
	});
	
    
    
}

var requeststarr=new Array();
requeststarr[1]="Accepted";
requeststarr[0]="Blocked";
requeststarr[4]="Rejected";
function changeStatusFriendRequest(status,recordid,rowid)
{
    if(recordid>0 && document.getElementById('requeststatus_'+rowid))
    {
        $.ajax({
          cache:false,  
          async:false,
          url: "ajax_search/ajax_manageRequests.php?action=changereqstatus&recordid="+recordid+"&status="+status,
    	  success: function( data ) {
    	    if(trim(data)=="success")
            {
                document.getElementById('requeststatus_'+rowid).innerHTML="<b>"+requeststarr[status]+"</b>";
                return false;
            }
            
    	  }
    	});
    }
}

function blockFriend(displaybtn,blockbtn,status,recordid,touserid,rowid)
{
    if(recordid>0)
    {
        
        $.ajax({
          cache:false,  
          async:false,
          url: "ajax_search/ajax_manageRequests.php?action=blockunblocreqstatus&userrecordid="+recordid+"&status="+status+"&touserid="+touserid,
    	  success: function( data ) {
    	   
    	    if(trim(data)=="success")
            {
                document.getElementById(displaybtn+rowid).style.display='';
                document.getElementById(blockbtn+rowid).style.display='none';
                return false;
            }
            
    	  }
    	});
    }
}


function getFilteredUsers(fieldid)
{
    if(trim(document.getElementById(fieldid).value)=="")
    {
        alertify.alert("Please enter "+fieldid);
        return false;
    }
    else
    {
        document.mainform.submit();
    }
}
//]]>
</script>